﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;


namespace AUV5.Web.Pages
{
    public partial class ServiceDetail : PageBaseFrontend
    {
        public string serviceTitle = HttpContext.Current.Request["serviceTitle"] ?? "";

        public string sellerUrl = string.Empty;
        public string sellerName = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["extralist"] = new ArrayList();
                if (!string.IsNullOrEmpty(Request["serviceTitle"]))
                {
                    serviceTitle = Request["serviceTitle"];
                    BindData(serviceTitle);
                }
                else
                {
                    Response.Redirect("~/Pages/Index.aspx");
                }
            }

        }

        #region##绑定服务信息
        /// <summary>
        /// 绑定服务信息
        /// </summary>
        /// <param name="serviceTitle"></param>
        protected void BindData(string serviceTitle)
        {
            //服务信息
            AUV5.Model.ServiceInfo service = AUV5.BLL.ServiceInfoBLL.Instance.GetDetailByTitle(serviceTitle);

            if (service != null)
            {
                //更新浏览量
                service.ViewCount++;

                AUV5.BLL.ServiceInfoBLL.Instance.UpdateServiceInfo(service);

                //卖家信息
                AUV5.Model.UserInfo user = AUV5.BLL.UserInfoBLL.Instance.GetDetail(service.UserID);

                if (user != null)
                {
                    //----------------------------------以下是用户信息显示部分---------------------------------------------------
                    imgSeller.ImageUrl = user.PhotoPath;        //用户头像
                    lblDesc.Text = user.Description;            //用户描述
                    sellerName = user.UserName;    //用户名
                    lblUserJoin.Text = base.GetTimeSpan(user.CreateDate);        //用户加入时间
                    lblLastLogTime.Text = base.GetTimeSpan(user.LastLogin);      //最后登陆时间
                    lbGender.Text = (user.Gender == (int)Common.Enum.Gender.Man) ? "男" : "女";
                    lbAddress.Text = BLL.GBADBLL.GetInstance().GetNameByCode(user.Province) + " " + BLL.GBADBLL.GetInstance().GetNameByCode(user.City);
                    int totalRateCount = AUV5.BLL.ServiceInfoBLL.Instance.GetRateSumByUserId(user.UserID);
                    imgUserLevel.ImageUrl = new AUV5.BLL.AuvLevelBLL().GetUserLevelByUserId(user.UserID);

                    if (totalRateCount > 0)
                    {
                        double receRate = Convert.ToDouble(AUV5.BLL.ServiceInfoBLL.Instance.GetGoodRateSumByUserId(user.UserID)) / Convert.ToDouble(totalRateCount);
                        lblReceivedRateSum.Text = receRate.ToString("0%");
                    }
                    else
                    {
                        lblReceivedRateSum.Text = "-";
                    }

                    //edit by amos 20111103
                    sellerUrl = "/" + user.UserName;
                    //lbtnSellerUserName.PostBackUrl = "UserHome.aspx?username=" + user.UserName;

                    string contractUrl = "~/Pages/MessageShow.aspx?userID=" + user.UserID + "&serviceID=" + serviceTitle;

                    if (base.IsVerified)
                    {
                        lbtnContract.NavigateUrl = contractUrl;
                    }
                    else
                    {
                        lbtnContract.NavigateUrl = "~/Pages/Login.aspx?returnURL=" + contractUrl;
                    }
                }


                //-----------------------------------------以下是服务信息显示部分------------------------------------------------

                int totalRate = service.GoodRating + service.BadRating;
                if (totalRate != 0)                                          //好评率
                {
                    double receRate = Convert.ToDouble(service.GoodRating) / Convert.ToDouble(totalRate);
                }
                if (base.IsVerified)                               //收藏
                {
                    if (base.VerifiedUserID != service.UserID)
                    {
                        bool flag = AUV5.BLL.FavoriteBLL.Instance.Contain(serviceTitle, base.VerifiedUserID);
                    }
                    else
                    {
                        lbtnContract.Visible = false;
                    }
                }

                UCService1.ServiceID = service.ServiceID;


                bindUserRelated(service.ServiceID);  //用户其它服务

                bindKeyRelated(service.ServiceID);  //其它服务  

                BindFeebBack();  //绑定留言
            }
        }
        #endregion

        #region##绑定留言
        /// <summary>
        /// 绑定留言
        /// </summary>
        public void BindFeebBack()
        {
            string sqlwhere = "ServiceID ='" + serviceTitle + "' and State = " + Convert.ToInt32(AUV5.Common.Enum.auvState.Enabled) + " and IsDel = " + Convert.ToInt32(AUV5.Common.Enum.auvDel.NotDel);
            string orderby = "CreateDate desc,FeedBackID desc";
            int recordcount = 0;
            IList<AUV5.Model.FeedBack> list = AUV5.BLL.FeedBackBLL.Instance.GetPagedFeedBack(AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, sqlwhere, orderby, ref recordcount);
            if (list.Count == 0)
            {
                pnlFeedBack.Visible = false;
            }
            rpFeedBack.DataSource = list;
            rpFeedBack.DataBind();
            AspNetPager1.RecordCount = recordcount;
        }
        #endregion

        #region##绑定该用户的其它信息
        /// <summary>
        /// 绑定该用户的其它信息
        /// </summary>
        private void bindUserRelated(string serviceId)
        {

            AUV5.Model.ServiceInfo service = BLL.ServiceInfoBLL.Instance.GetDetail(serviceId);

            if (service != null)
            {
                string userId = service.UserID;
                repUserRelated.DataSource = AUV5.BLL.ServiceInfoBLL.Instance.GetUserServiceElseByUserId("10", userId, serviceId);
                repUserRelated.DataBind();
            }
        }
        #endregion

        #region##绑定该信息的相关信息
        /// <summary>
        /// 绑定该信息的相关信息
        /// </summary>
        private void bindKeyRelated(string serviceID)
        {
            //获取所有关键字列表
            string keywords = BLL.ServiceInfoBLL.Instance.GetTagList(serviceID);
            if (keywords != "")
            {
                //获取与该关键字相关的信息ID列表
                string idlist = BLL.ServiceKeyRelaBLL.Instance.GetListByKeyList(keywords.Split(','), serviceID, 10);
                //根据ID列表获取所有信息
                if (idlist != "")
                {
                    Model.NetPager pager = new Model.NetPager();
                    pager.PageIndex = 1;
                    pager.TableName = "ServiceInfo";
                    pager.PrimaryKey = "ServiceID";
                    pager.Where = "ServiceID IN (" + idlist + ") and State = " + Convert.ToInt32(AUV5.Common.Enum.auvState.Enabled) + " and IsDel = " + Convert.ToInt32(AUV5.Common.Enum.auvDel.NotDel); ;
                    pager.Order = "UpdateDate Desc,ServiceID asc";
                    pager.SortType = (int)AUV5.Common.Enum.SortType.Multi;
                    pager.PageSize = 10;
                    pager.RecorderCount = 10;
                    repKeyRelated.DataSource = BLL.ServiceInfoBLL.Instance.GetServiceInfoListPage(pager);
                    repKeyRelated.DataBind();


                }
            }
        }
        #endregion

        #region##服务信息repeater项绑定后激发事件
        /// <summary>
        /// 服务信息repeater项绑定后激发事件
        /// 创建人：Wilson
        /// 创建时间：2011-10-26
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void rpFeedBack_DataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                Label lblId = e.Item.FindControl("lblId") as Label;  //主键
                //Image imgFbUser = e.Item.FindControl("imgFbUser") as Image; //留言人头像
                LinkButton lbtnFbUserName = e.Item.FindControl("lbtnFbUserName") as LinkButton; //留言人姓名
                Label lblFbTime = e.Item.FindControl("lblFbTime") as Label;       //留言时间
                Image imgFbIsGood = e.Item.FindControl("imgFbIsGood") as Image;  //是否好评
                Panel pnlReply = e.Item.FindControl("pnlReply") as Panel;  //回复层
                //Image imgSeller = e.Item.FindControl("imgSeller") as Image;  //卖家头像
                //Label lblSellerName = e.Item.FindControl("lblSellerName") as Label;  //卖家名
                Label lblReplyTime = e.Item.FindControl("lblReplyTime") as Label;  //卖家回复时间


                AUV5.Model.FeedBack fb = AUV5.BLL.FeedBackBLL.Instance.GetDetail(lblId.Text);

                if (fb != null)
                {
                    AUV5.Model.UserInfo buyer = AUV5.BLL.UserInfoBLL.Instance.GetDetail(fb.UserId);
                    if (buyer != null)
                    {
                        //imgFbUser.ImageUrl = buyer.PhotoPath;
                        lbtnFbUserName.Text = buyer.UserName;
                        lblFbTime.Text = base.GetTimeSpan(fb.CreateDate);
                        imgFbIsGood.ImageUrl = fb.IsGood ? "/!images/icon_handgood.jpg" : "/!images/icon_handbad.jpg";

                        if (!string.IsNullOrEmpty(fb.Reply))
                        {
                            //服务信息
                            AUV5.Model.ServiceInfo service = AUV5.BLL.ServiceInfoBLL.Instance.GetDetail(fb.ServiceID);

                            AUV5.Model.UserInfo seller = AUV5.BLL.UserInfoBLL.Instance.GetDetail(service.UserID);

                            if (seller != null)
                            {
                                //imgSeller.ImageUrl = seller.PhotoPath;
                                //lblSellerName.Text = seller.UserName;
                                lblReplyTime.Text = base.GetTimeSpan(fb.ReplyDate);
                            }
                        }
                        else
                        {
                            pnlReply.Visible = false;
                        }
                    }
                }
            }
        }
        #endregion

        #region##留言翻页
        /// <summary>
        /// 留言翻页
        /// 创建人：Wilson
        /// 创建时间：2011-10-31
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindFeebBack();
        }
        #endregion

    }

}